package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.file.DeleteFileBo;
import com.zhuss.bean.vo.core.file.DetailFileVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.FileDao;
import com.zhuss.core.db.entity.File;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.util.JsonUtil;

/**
 * 删除文件 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/26 21:55
 */
@Component("fileDelete")
public class FileDelete extends AbstractProcessor<Object, DeleteFileBo> implements Processor<Object, DeleteFileBo> {

    @Autowired
    private FileDao fileDao;
    
    @Autowired
	@Qualifier("fileRead")
	private FileRead fileRead;
    
    /**
     * 校验
     */
	@Override
    public void check(DeleteFileBo bo) throws OceanException {
		
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteFileBo param) throws OceanException {
    	String key = param.getFileKey();
    	Long fileId = ((DetailFileVo) fileRead.getByKey(key)).getFileId();
    	param.setFileId(fileId);
    	
    	File record = new File();
    	record.setFileId(fileId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	fileDao.delete(record);
    	param.setName(key);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteFileBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.文件数据删除.key());
    	record.setTargetId(param.getFileId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
